/**
 * Created with  SublimeText
 * @Author:      Lyle
 * @DateTime:    2017-11-09 09:43:03
 * @Description: 基础的自动化打包
 */

module.exports = function(grunt) {
    var md5 = require('md5');
    var os = require('os');

    grunt.initConfig({

        concat: {
            options: {
                separator: ';'
            },
            target: {
                src: ['src/assets/lib/seajs/sea-debug.js',
                    'src/assets/lib/seajs/seajs-css.js',
                    'src/assets/lib/seajs/seajs-text.js',
                    'src/assets/lib/md5/md5.js',
                    'src/html/main.js'
                ],
                dest: 'src/assets/lib/seajs/seajs.js'
            }
        },

        uglify: {
            options: {
                mangle: false,
            },
            target: {
                files: [{
                    expand: true,
                    cwd: 'src',
                    src: ['**/*.js', '!**/*.min.js'],
                    dest: 'dist'
                }],
            },
            cdn: {
                files: [{
                    expand: true,
                    cwd: 'src',
                    src: ['**/*.js', '!**/*.min.js'],
                    dest: 'dist',
                    rename: function(dest, src) {
                        if (src.indexOf('seajs.js') > -1) {
                            return dest + '/' + src;
                        } 
                        var version = grunt.config.getRaw('version');
                        src = src.replace(/(.*\/)(.+)(\.(css|js|tpl))$/i, function(src, path, name, ext) {
                            return path + '!@-' + md5(name + version) + ext;
                        });
                        return dest + '/' + src;
                    }
                }],
            },
        },

        cssmin: {
            target: {
                files: [{
                    expand: true,
                    cwd: 'src',
                    src: '**/*.css',
                    dest: 'dist',
                }]
            },
            cdn: {
                files: [{
                    expand: true,
                    cwd: 'src',
                    src: '**/*.css',
                    dest: 'dist/',
                    rename: function(dest, src) {
                        var version = grunt.config.getRaw('version');
                        src = src.replace(/(.*\/)(.+)(\.(css|js|tpl))$/i, function(src, path, name, ext) {
                            return path + '!@-' + md5(name + version) + ext;
                        });
                        return dest + '/' + src;
                    }
                }]
            }
        },

        imagemin: {
            target: {

                options: {
                    //优化级别1-7，默认是3，效果不明显                        
                    optimizationLevel: 3,
                },

                files: [{
                    expand: true,
                    cwd: "src",
                    src: ['**/*.{png,jpg,gif}'],
                    dest: "dist"
                }]
            }
        },

        copy: {

            target: {
                expand: true,
                cwd: 'src',
                src: ['**/*.min.js'],
                dest: 'dist',
            },


            cdn: {
                expand: true,
                cwd: 'src',
                src: ['**/*.min.js'],
                dest: 'dist',
                rename: function(dest, src) {
                    var version = grunt.config.getRaw('version');
                    src = src.replace(/(.*\/)(.+)(\.(css|js|tpl))$/i, function(src, path, name, ext) {
                        return path + '!@-' + md5(name + version) + ext;
                    });
                    return dest + '/' + src;
                }
            },

            tpl: {
                expand: true,
                cwd: 'src',
                src: ['**/*.tpl'],
                dest: 'dist/',
                options: {
                    process: function(content, srcpath) {
                        return content.replace(/[\r\n]/g, " ").replace(/\s*>/g, '>').replace(/>\s+</g, '> <').replace(/\s+/g, ' ').replace(/<!--.*?-->/g, '');
                    },
                },
                rename: function(dest, src) {
                    var version = grunt.config.getRaw('version');
                    src = src.replace(/(.*\/)(.+)(\.(css|js|tpl))$/i, function(src, path, name, ext) {
                        return path + '!@-' + md5(name + version) + ext;
                    });
                    return dest + '/' + src;
                }
            },
        },

        htmlmin: {
            target: {
                options: {
                    removeComments: true,
                    collapseWhitespace: true,
                    minifyJS: true,
                    minifyCSS: true
                },
                files: [{
                    expand: true,
                    cwd: 'src',
                    src: ['**/*.html'],
                    dest: 'dist',
                }]

            }
        },

        shell: {
            options: {
                stderr: false,
                execOptions: {
                    maxBuffer: 2000 * 1024
                },
            },
            remove: {
                command: 'rm -rf ./dist/*'
            }
        }

    });

    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-htmlmin');
    grunt.loadNpmTasks('grunt-contrib-imagemin');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-shell');

    grunt.registerTask('build-seajs', 'build seajs', function(arg) {
        grunt.task.run(['concat:target'])
    });

    //eg:grunt build:1.0
    grunt.registerTask('build', 'Automated package', function(arg) {
        if (arguments.length > 0) {
            grunt.config('version', arg);
            grunt.config('seajsName', 'z@' + md5('seajs' + arg));
            grunt.task.run(['shell:remove', 'concat:target', 'uglify:cdn', 'cssmin:cdn', 'imagemin:target', 'copy:cdn', 'copy:tpl', 'htmlmin:target']);
            grunt.log.writeln("Package, the current version is modified to v" + arg);
        }
    });

};